package com.example.p2pinvest.fragment;

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.p2pinvest.R;
import com.example.p2pinvest.common.BaseFragment;
import com.example.p2pinvest.ui.randomLayout.StellarMap;
import com.example.p2pinvest.util.UIUtils;
import com.loopj.android.http.RequestParams;

import java.util.Random;

import butterknife.BindView;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/8/10
 * 推荐理财
 * 如何在布局中添加子视图
 * 1.直接在布局文件中以标签的方式添加 ---静态
 * 2.在java代码中动态添加子视图   ---> addView()/adapter+holder/adapter+viewPager
 */
public class ProductRecommendFragment extends BaseFragment {

    @BindView(R.id.stellar_map)
    StellarMap stellarMap;

    //提供装配的数据
    private String[] datas = new String[]{"新手福利计划", "财神道90天计划", "硅谷钱包计划", "30天理财计划(加息2%)", "180天理财计划(加息5%)", "月月升理财计划(加息10%)",
            "中情局投资商业经营", "大学老师购买车辆", "屌丝下海经商计划", "美人鱼影视拍摄投资", "Android培训老师自己周转", "养猪场扩大经营",
            "旅游公司扩大规模", "铁路局回款计划", "屌丝迎娶白富美计划","摩托罗拉洗钱计划"
    };

    //声明两个子数组
    private String[] oneDatas = new String[datas.length / 2];
    private String[] twoDatas = new String[datas.length - datas.length / 2];

    private Random random = new Random();

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return null;
    }

    @Override
    protected void initData(String content) {

        for (int i = 0; i < datas.length; i++) {
            if (i < datas.length / 2) {
                oneDatas[i] = datas[i];
            } else {
                twoDatas[i - datas.length / 2] = datas[i];
            }
        }

        stellarMap.setAdapter(new StellarAdapter());

        //设置字体显示内边距
        int padding = UIUtils.dp2px(5);
        stellarMap.setInnerPadding(padding, padding, padding, padding);

        //必须调用一下的两个方法
        //设置显示的数据在x轴,y轴方向上的稀疏度
        stellarMap.setRegularity(5, 7);
        //设置初始化显示的组别以及初始动画
        stellarMap.setGroup(0, true);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_productrecomment;
    }


    class StellarAdapter implements StellarMap.Adapter {

        //获取组的个数
        @Override
        public int getGroupCount() {
            return 2;
        }

        //返回每组中显示的个数
        @Override
        public int getCount(int group) {
            if (group == 0) {
                return datas.length / 2;
            } else {
                return datas.length - datas.length / 2;
            }
        }

        //不同的组别,position都是从0开始的
        @Override
        public View getView(int group, int position, View convertView) {
            TextView tv = new TextView(getActivity());

            //设置文本内容
            if (group == 0) {
                tv.setText(oneDatas[position]);
            } else {
                tv.setText(twoDatas[position]);
            }
            //设置字体大小
            tv.setTextSize(UIUtils.dp2px(5) + UIUtils.dp2px(random.nextInt(5)));
            //设置字体颜色
            int red = random.nextInt(211);  // 00-ff 0-255
            int green = random.nextInt(211);
            int blue = random.nextInt(211);
            tv.setTextColor(Color.rgb(red, green, blue));

            //设置点击事件
            tv.setOnClickListener(v -> {
                UIUtils.toast(tv.getText().toString(), false);
            });
            return tv;
        }

        //返回下一组显示平移动画的组别,源码中此方法不被调用
        @Override
        public int getNextGroupOnPan(int group, float degree) {
            return 0;
        }

        //显示缩放动画的组别
        @Override
        public int getNextGroupOnZoom(int group, boolean isZoomIn) {
            if (group == 0) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}

